﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Text.RegularExpressions;
using FinPlusCommon;

namespace FinPlusWPFControls
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueFormatTextBox : FinPlusControl
    {
        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public override string[] States { get { return new string[] { "TestString", "ViewString", "IsValid" }; } }
        public override string TestString { get { return string.Format("{0}|{1}", IsEnabled, Text); } }

        public string ViewString { get { return string.Format("{0}|{1}", textBox.IsEnabled, textBox.Text); } }
        public string FunctionName { get { return GetValue(FunctionNameProperty).ToString(); } set { } }
        public string Text { get { return GetValue(TextProperty).ToString(); } set { SetValue(TextProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }
        public string StringFormat { get { return GetValue(StringFormatProperty).ToString(); } }
        public Regex InputMask { get { return (Regex)GetValue(InputMaskProperty); } }
        public string Type { get { return GetValue(TypeProperty).ToString(); } }
    
        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(Propertys.Id.ToString(), typeof(string), typeof(FinPlusValueFormatTextBox));
        public static readonly DependencyProperty FunctionNameProperty = DependencyProperty.Register(Propertys.FunctionName.ToString(), typeof(string), typeof(FinPlusValueFormatTextBox));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(Propertys.Text.ToString(), typeof(string), typeof(FinPlusValueFormatTextBox), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueFormatTextBox));
        public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register(Propertys.StringFormat.ToString(), typeof(string), typeof(FinPlusValueFormatTextBox));
        public static readonly DependencyProperty InputMaskProperty = DependencyProperty.Register(Propertys.InputMask.ToString(), typeof(Regex), typeof(FinPlusValueFormatTextBox));
        public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(Propertys.Type.ToString(), typeof(string), typeof(FinPlusValueFormatTextBox));

        private enum Propertys { Id, FunctionName, Text, IsEnabled, StringFormat, InputMask, Type, Num }
        private bool _hasSetEnable;

        //construct
        public FinPlusValueFormatTextBox()
        {
            InitializeComponent();
            IsValid = true;

            textBox.Cursor = ((TextBlock)this.Resources["CPoint"]).Cursor;
        }

        //public
        public void SetControl()
        {
            //permission
            if (!_hasSetEnable)
            {
                textBox.IsReadOnly = !(IsEnabled && GetPermission(FunctionName, Propertys.Id.ToString(), Id).CanWrite);
                _hasSetEnable = true;
            }

            //format
            double v;
            if (Type !=null && Type.Equals(Propertys.Num.ToString()) && double.TryParse(Text, out v))
            {
                textBox.Foreground = v < 0 ? Brushes.Red : Brushes.Black;
                if (textBox.TextAlignment != TextAlignment.Right)
                    textBox.TextAlignment = TextAlignment.Right;
            }
        }

        //events
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueFormatTextBox)d).SetControl(); }
        private void Text_PreviewKeyDown(object s, KeyEventArgs a) { }//TODO
        private void Text_PreviewTextInput(object s, TextCompositionEventArgs a) { if (InputMask != null && !InputMask.IsMatch(a.Text))  a.Handled = true; }
    }
}